package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.EventEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: UpdateEqpStatus
 * Author: XJH
 * Date: 2022/1/18 12:41
 * Description:
 * History:
 */
public class UpdateEqpStatus extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.tvEqpPosition)
    TextView tvEqpPosition;
    @BindView(R.id.tvStatusMain)
    TextView tvStatusMain;
    @BindView(R.id.tvStatusItem)
    TextView tvStatusItem;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.flEventList)
    FrameLayout flEventList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvEvent)
    TextView tvEvent;
    @BindView(R.id.ivEventArrow)
    ImageView ivEventArrow;
    @BindView(R.id.tvTargetStatus)
    TextView tvTargetStatus;
    @BindView(R.id.etComment)
    EditText etComment;

    private FreeUI_GeneralPop mEventPop;
    private List<EventEntity.LISTEVENTS> mEventList;
    private List<String> mEventIDList;
    private String codes = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_update_eqp_status;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("修改设备状态");
        mEventIDList = new ArrayList<>();
        mEventList = new ArrayList<>();
        initEdit(etEqp, s -> LotUtils.searchEqpInfo(s, UpdateEqpStatus.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
            @Override
            public void onSuccess(EqpInfoEntity response) {
                tvEqpPosition.setText(response.getEQUIPMENT().getLOCATION());
                tvStatusMain.setText(response.getEQUIPMENT().getCOMCLASS());
                tvStatus.setText(response.getEQUIPMENT().getSTATE());
                tvStatusItem.setText(response.getEQUIPMENT().getSUBSTATE());
                requestEvent(s);

            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"EQPQUERY");
            }
        }));
    }

    private void requestEvent(String s) {
        FreeUI_EntityCallBack<BaseResponseBean<EventEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EventEntity>>
                (new TypeToken<BaseResponseBean<EventEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EventEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EventEntity body = response.body().getResponse().getBody();
                    mEventList.clear();
                    mEventIDList.clear();
                    mEventList.addAll(body.getLISTEVENTS());
                    for (EventEntity.LISTEVENTS listevents : mEventList) {
                        mEventIDList.add(listevents.getEVENTID());
                    }

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EventEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOGEVENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERYEVENT");
        map.addProperty("EQUIPMENTID",s);
        NetUtils.requestPostNet("CPC.LOGEVENT", "TRANSFERMES",this, map, callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.flEventList, R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flEventList:
                if (mEventIDList.size() <= 0){
                    showToast("未获取到事件列表",false);
                    return;
                }
                //维修故障类型
                if (mEventPop != null) {
                    if (mEventPop.isShowing()) {
                        mEventPop.dismiss();
                    }
                } else {
                    mEventPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEventList.getWidth(), mEventIDList, ivEventArrow, (codeS, i) -> {
                        fillData(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivEventArrow);
                mEventPop.showAsDropDown(flEventList, 0, 0);
                break;
            case R.id.tvBaseSubmit:
                if (etEqp.getText().toString().trim().isEmpty()){
                    showToast("请先扫描设备号",false);
                    return;
                }
                if (tvEvent.getText().toString().trim().isEmpty()){
                    showToast("请选择事件",false);
                    return;
                }

                if (etComment.getText().toString().trim().isEmpty()){
                    showToast("请输入备注",false);
                    return;
                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                                    showConfirmDialog(UpdateEqpStatus.this,"修改成功");
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(), "LOGEVENT");
                            }
                        };

                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE","TRIGGER");
                        map.addProperty("EQUIPMENTID",etEqp.getText().toString().trim());
                        map.addProperty("EVENTID",codes);
                        map.addProperty("COMMENT",etComment.getText().toString().trim());
                        NetUtils.requestPostNet("CPC.LOGEVENT","TRANSFERMES",this,map,callBack);
                    }
                });
                break;
        }
    }

    private void fillData(String codeS) {
        codes = codeS;
        for (EventEntity.LISTEVENTS listevents : mEventList) {
            if (codeS.equals(listevents.getEVENTID())){
                tvEvent.setText(listevents.getDESCRIPTION());
                if (!listevents.getEVENTSTATUSLIST().isEmpty()){
                    tvTargetStatus.setText(listevents.getEVENTSTATUSLIST().get(0).getTargetComClass() + " > " + listevents.getEVENTSTATUSLIST().get(0).getTargetState());
                }
                return;
            }
        }
    }
}
